<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">后台管理系统</h1>
        <h2 class="login-title">用户登录</h2>
        <el-form :model="loginForm" label-width="100px"
                 :rules="rules" ref="loginForm">
          <el-form-item label="账号" prop="username">
            <el-input style="width: 200px" type="text" v-model="loginForm.username"
                      autocomplete="off" size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input style="width: 200px" type="password" v-model="loginForm.password"
                      show-password autocomplete="off" size="small" @keyup.enter.native="confirm"></el-input>
          </el-form-item>
          <!--          <el-form-item label="验证码">-->
          <!--            <verify :showButton="false" ref="verify"></verify>-->
          <!--          </el-form-item>-->
          <el-form-item prop="code" label="验证码" style="width: 300px">
            <el-input placeholder="请输入验证码" prefix-icon="el-icon-circle-check"  style="flex: 1 "
                      v-model="loginForm.code" @keyup.enter.native="confirm"></el-input>
            <div style="flex: 1; height: 40px">
              <valid-code ref="validCode" @update:value="getCode" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirm" :disabled="confirm_disabled">确 定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<!--<el-input type="text" v-model="inputCode"></el-input>-->

<script>
// import Verify from 'vue2-verify'
export default {
  components:{
    // Verify,
    ValidCode: () => import('../components/ValidCode.vue')
  },
  name: "Login",
  data(){
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    return{
      confirm_disabled:false,
      loginForm:{
        username:'',
        password:'',
        code:''
      },
      //code:'',
      rules:{
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输密码', trigger: 'blur' }
        ],
        code: { validator: validateCode, trigger: 'blur' }
      }
    }
  },
  methods:{
    reloadCaptcha() {
    },
    getCode(code) {
      console.log(code);
      this.code = code.toLowerCase()
    },
    refreshCode() {
      this.$refs.validCode.createdCode()
    },
    confirm(){

      this.confirm_disabled=true;

      this.$refs.loginForm.validate((valid) => {
        // 刷新验证码
        this.refreshCode(); // 调用刷新验证码方法
        if (valid) { //valid成功为true，失败为false

          //去后台验证用户名密码
          this.$axios.post(this.$httpUrl+'/user/login',this.loginForm).then(res=>res.data).then(res=>{
            console.log(res)
            if(res.code==200){
              //存储
              sessionStorage.setItem("CurUser",JSON.stringify(res.data.user))
              console.log(res.data.menu)
              this.$store.commit("setMenu",res.data.menu)
              //跳转到主页
              this.$router.replace('/Index');
            }else{
              this.confirm_disabled=false;
              alert('校验失败，用户名或密码错误！');
              return false;
            }
          });
        } else {
          this.confirm_disabled=false;
          console.log('校验失败');
          return false;
        }
      });

    }
  }
}
</script>

<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url('../assets/img.png');
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 450px;
  background: rgba(255, 255, 255, 100);
  border-radius: 5%;
}
.login-title {
  margin: 20px 0;
  text-align: center;
}
.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
</style>